const mongoose = require("mongoose");
var Merchant = mongoose.model("merchant");

exports.findAllMerchant = function(req, res) {
    Merchant.find(function(err, merchants) {
        res.json(merchants)
    });
}

exports.passStatus = function(req, res) {
    Merchant.findOne({ mId: req.params.id }, function(err, merchant) {

        merchant.mStatus = 2;
        merchant.save(function(err) {
            console.log("  req.params.id :  " + req.params.id)
            console.log(merchant)
            if (!err) {

                res.send("success");
            } else {
                res.json({ error: "passStatus" });
            }
        })

    })

}
exports.refuseStatus = function(req, res) {
    Merchant.findOne({ mId: req.params.id }, function(err, merchant) {

        merchant.mStatus = 3;
        merchant.save(function(err) {
            console.log("  req.params.id :  " + req.params.id)
            console.log(merchant)
            if (!err) {

                res.send("success");
            } else {
                res.json({ error: "refuseStatus" });
            }
        })

    })

}

exports.lowMerchant = function(req, res) {
    Merchant.find({ mScore: { $lte: 5 } }, function(err, merchants) {
        if (!err) {
            res.json(merchants);
        } else {
            res.json({ error: "lowMerchant" });
        }
    })
}

exports.blackMerchant = function(req, res) {
    Merchant.findOne({ mId: req.params.mId }, function(err, merchant) {
        merchant.mStatus = 4;
        merchant.save(function(err) {
            if (!err) {
                res.send("success");
            } else {
                res.send("fail");
            }
        });
    });
}

exports.showTelList = function(req, res) {
    Merchant.find({ mStatus: { $in: [2, 4] } }, function(err, merchants) {
        if (!err) {
            var mTels = [];
            merchants.forEach(function(item) {
                mTels.push(item.mTel);
            });
            res.send(mTels);
        } else {
            res.json({ error: "showtelList" })
        }
    });
}

exports.findMerchantByTel = function(req, res) {
    Merchant.findOne({ mTel: req.params.input_tel }, function(err, merchant) {
        if (!err) {
            console.log(merchant)
            res.json(merchant);
        } else {

            res.json({ error: "findMerchantByTel" })
        }
    })
}
exports.showMerchantWS = function(req, res) {////odd protocol
    Merchant.find({ mStatus: 2 }, function(err, merchants) {
        // var mIds = [];
        var mIds="";
        merchants.forEach(function(item) {
            // mIds.push(item.mId);
            mIds=mIds+item.mId+",";
        })
        res.send(mIds);
    })
}

exports.showMerchantByIdWS = function(req, res) {
    Merchant.findOne({ mId: req.params.id }, function(err, merchant) {
        res.send(merchant.mStatus + "");
    })
}
exports.changeMerchantStatus = function(req, res) {
    Merchant.findOne({ mId: req.params.mId }, function(err, merchant) {
        merchant.mStatus=parseInt(req.params.status);
        merchant.save(function(err){
            if(err){
                res.send("fail");
            }
            else{res.send("success")}
        })
    })
}
